<template>
  <div class="toast" v-show="isShow">
    <div>{{ message }}</div>
  </div>
</template>

<script>
export default {
  name: "Toast",
  data() {
    return {
      message: "",
      isShow: false,
      clock: false,
    };
  },
  methods: {
    //message="默认文字",duration=默认值，若不传参数则使用默认
    show(message = "默认文字", duration = 1500) {
      this.isShow = true;
      this.message = message;

      /* 解决定时器触发频率问题，每次点击就会清除上一个定时器 */
      if (this.isShow) { 
        clearTimeout(this.clock);
      }
      this.clock = setTimeout(() => {
        this.isShow = false;
        this.message = "";
      }, duration);
    },
  },
  components: {},
};
</script>

<style  scoped>
.toast {
  position: fixed;
  top: 80%;
  left: 50%;
  transform: translate(-50%);
  font-size: 14px;
  text-align: center;
  background-color: rgb(255, 129, 152);
  border-radius: 5px;
  color: white;
  z-index: 50;
  padding: 5px;
}
</style>
